package com.dissertation.action;

import com.dissertation.annotation.CheckBindingResult;
import com.dissertation.model.Assets;
import com.dissertation.model.ScientificProject;
import com.dissertation.model.auth.User;
import com.dissertation.pojo.IdsVO;
import com.dissertation.pojo.Message;
import com.dissertation.pojo.Param;
import com.dissertation.respository.AssetsRepository;
import com.dissertation.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping( "assets" )
public class AssetsCommonController {

    @Autowired
    IAssetsFindByTag findByTagService;

    @Autowired
    AssetsService assetsService;


    @GetMapping("authorAndType")
    public Message findByAuthorAndType(@RequestParam String name,@RequestParam String categoryLeafName){
        if(name==null||categoryLeafName==null){
            return Message.createErr("参数不能为空");
        }
        return Message.createSuc(assetsService.findByAuthorAndType(name,categoryLeafName));
    }



    @GetMapping("participantAndType")
    public Message findByParticipantAndType(@RequestParam String name,@RequestParam String categoryLeafName){
        if(name==null||categoryLeafName==null){
            return Message.createErr("参数不能为空");
        }
        return Message.createSuc(assetsRepository.findByParticipantNamesContainsAndDeletedIsFalseAndCategoryLeafName(name,categoryLeafName));
    }




    @GetMapping("name")
    public Message findByName(@RequestParam String name){
        if(name==null||"".equals(name)){
            return Message.createErr("参数不能为空");
        }
        return Message.createSuc(assetsRepository.findByNameAndDeletedIsFalse(name));
    }



    @GetMapping( "tags" )
    Message findByTag( String tagName,
                       String categoryLeafName,
                       String categoryTreeName ) {

        if ( tagName == null || categoryLeafName == null || categoryTreeName == null ) {
            return Message.createErr( "参数不能为空" );
        }

        return Message.createSuc(
                findByTagService.findByTag( tagName, categoryTreeName, categoryLeafName )
        );
    }


    @Autowired
    IAssetsFindByUser assetsFindByUserService;

    @GetMapping( "participant" )
    Message findByParticipant( @RequestParam( defaultValue = "" ) String name ) {

        return Message.createSuc( assetsFindByUserService.findByParticipant( name ) );
    }

    @GetMapping( "relative" )
    Message findByRelative( @RequestParam( defaultValue = "" ) String name ) {
        return Message.createSuc( assetsFindByUserService.findByRelative( name ) );
    }

    @GetMapping( "author" )
    Message findByAuthor( @RequestParam( defaultValue = "" ) String name ) {
        return Message.createSuc( assetsFindByUserService.findByAuthor( name ) );
    }

    @Autowired
    IAssetsFindByKeyword findByKeywordService;

    /**
     * @param keyword1
     * @param keyword2
     * @param op       true 为 and， false 为or
     * @return
     */
    @GetMapping( "keyword" )
    Message findByKeyWord( @RequestParam( defaultValue = "" ) String keyword1,
                           @RequestParam( defaultValue = "%%%%" ) String keyword2,
                           @RequestParam( defaultValue = "false" ) boolean op ) {
        return Message.createSuc( findByKeywordService.findByKeyword(
                keyword1, keyword2, op
        ) );
    }

    @PostMapping("keyword")
    Message findByKeyWork(@RequestBody Param param) {
        return Message.createSuc(findByKeywordService.findByParam(param));
    }

    @PostMapping("keyword/teacher")
    public Message findByKeyWordTeacher(@RequestBody Param param){
        List<Assets> assetsList = findByKeywordService.findByParam(param);
        assetsList.forEach(e->{
            if("学生科研".equals(e.getCategoryTreeName())){
                assetsList.remove(e);
            }
        });
        return Message.createSuc(assetsList);
    }


    @PostMapping("keyword/student")
    public Message findByKeyWordStudent(@RequestBody Param param){
        List<Assets> assetsList = findByKeywordService.findByParam(param);
        assetsList.forEach(e->{
            if(!("学生科研".equals(e.getCategoryTreeName())||"实习基地".equals(e.getCategoryLeafName())||"实验室".equals(e.getCategoryLeafName()))){
                assetsList.remove(e);
            }
        });
        return Message.createSuc(assetsList);
    }





    /**
     * 文档批量删除
     */
    @PostMapping("deleted")
    public Message deleteAssets(@RequestBody IdsVO idsVO){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        List<String> idList = Arrays.asList(idsVO.getIds());
        return assetsService.deleteAssets(idList);
    }

    @Autowired
    private AssetsRepository assetsRepository;

    @GetMapping("deleted")
    public Message deleteAssets(){
        return Message.createSuc(assetsRepository.findByDeletedIsTrue());
    }

    /**
     * 批量恢复删除
     * */
    @PostMapping("recover")
    public Message recoverAssets(@RequestBody IdsVO idsVO){
        List<String> idList = Arrays.asList(idsVO.getIds());
        return assetsService.recoverAssets(idList);
    }

}
